#include "showblog.h"

extern QString pathPre;

ShowBlog::ShowBlog(QWidget *parent, QMap<QString, QString> *mapInfo, int type)
{
    this->map = *mapInfo;
    qDebug() << map["blog_title"];
    //setStyleSheet(QString::fromUtf8("border:2px solid rgb(231,230,238)"));
    textContent = new QTextBrowser();
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setMargin(0);
    labelTitle = new PixLabel(15);
    labelTime = new PixLabel();
    labelPraise = new PixLabel();
    labelComment = new PixLabel();
    labelCollection = new PixLabel();

    labelTitle->changeLabel(QPixmap(map["pix"]), map["blog_title"]);
    labelTime->changeLabel(QPixmap(":/res/time.jpeg"), map["blog_time"]);
    labelPraise->changeLabel(QPixmap(":/res/dispraise.jpeg"), map["blog_praise"]);
    labelComment->changeLabel(QPixmap(":/res/comment.jpeg"), map["blog_comment"]);
    labelCollection->changeLabel(QPixmap(":/res/discollection.jpeg"), map["blog_collection"]);

    labelTitle->installEventFilter(this);
    labelPraise->installEventFilter(this);
    labelComment->installEventFilter(this);
    labelCollection->installEventFilter(this);

    labelList << labelTitle << labelTime << labelPraise << labelComment << labelCollection;
    strList << "title" << "time" << "praise" << "comment" << "collection";

    // 标题
    vLayout->addWidget(labelTitle);
    // 正文
    textContent->setMinimumHeight(1000);
    textContent->setStyleSheet("QTextBrowser{background-color:rgb(237,243,247);}");
    // 显示文档
    if(type == 0)
        textContent->setSource(QUrl::fromLocalFile(pathPre+map["blog_content"]));
    else
        textContent->setHtml(map["blog_content"]);
    vLayout->addWidget(textContent);

    // 底部
    QHBoxLayout *hLayout = new QHBoxLayout();
    QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Ignored);
    hLayout->setMargin(0);
    hLayout->addWidget(labelTime);
    hLayout->addSpacerItem(spacer);
    hLayout->addWidget(labelPraise);
    hLayout->addWidget(labelCollection);
    hLayout->addWidget(labelComment);
    QWidget *widget = new QWidget();
    widget->setLayout(hLayout);
    vLayout->addWidget(widget);

    // 加载整体布局
    setLayout(vLayout);
    this->commentNum = map["blog_comment"];
}

void ShowBlog::setPraise(bool isPraise, QString p)
{
    this->praiseNum = p;
    this->isPraise = isPraise;
    if(isPraise)
         labelPraise->changeLabel(QPixmap(":/res/praise.jpeg"), p);
    else
        labelPraise->changeLabel(QPixmap(":/res/dispraise.jpeg"), p);
}
void ShowBlog::setCollection(bool isCollection, QString c)
{
    this->collectionNum = c;
    this->isCollection = isCollection;
    if(isCollection)
        labelCollection->changeLabel(QPixmap(":/res/collection.jpeg"), c);
    else
        labelCollection->changeLabel(QPixmap(":/res/discollection.jpeg"), c);
}
void ShowBlog::setComment(QString c)
{
    this->commentNum = c;
    labelComment->changeLabel(QPixmap(":/res/comment.jpeg"), c);
}
QString ShowBlog::getPraise()
{
    return this->praiseNum;
}
QString ShowBlog::getCollection()
{
    return this->collectionNum;
}
QString ShowBlog::getComment()
{
    return this->commentNum;
}
bool ShowBlog::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<labelList.length(); i++)
    {
        if(obj == labelList.at(i))
        {
            // 鼠标进入
            if(event->type() == QEvent::Enter)
            {
                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
            }
            // 鼠标离开
            else if (event->type() == QEvent::Leave)
            {
                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(237,243,247);}");

            }
            // 鼠标单击事件
            else if(event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                // 左键单击
                if(mouseEvent->button() == Qt::LeftButton)
                {
                    // 发射信号
                    emit labelClicked(map, strList.at(i));
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

    return QObject::eventFilter(obj, event);
}

ShowBlog::~ShowBlog()
{

}
